<template>
  <el-card class="form-container" shadow="never">
    <el-form :model="menu" :rules="rules" ref="menuFrom" label-width="150px">
      <el-form-item label="菜单名称：" prop="title">
        <el-input v-model="menu.title"></el-input>
      </el-form-item>
      <el-form-item label="上级菜单：">
        <!-- <el-select v-model="menu.parentId"
                   placeholder="请选择菜单">

          <el-option :value="menu.parentId" :label="menu.title" style="width: 200px;height:200px;overflow: auto;background-color:#fff">
                    <el-tree
                            :data="selectMenuList"
                            :props="defaultProps"
                            @node-click="handleNodeClick"
                    ></el-tree>
                </el-option>

        </el-select>-->
        <treeselect
          v-model="selecttreelabel"
          :options="selectMenuList"
          :normalizer="normalizer"
          placeholder="选择上级类目"
          :clearable="false"
          @select="treeselectclick"
        ></treeselect>
      </el-form-item>
      <el-form-item label="前端名称：" prop="name">
        <el-input v-model="menu.name"></el-input>
      </el-form-item>
      <el-form-item label="前端图标：" prop="icon">
        <el-input v-model="menu.icon" style="width: 80%"></el-input>
        <svg-icon style="margin-left: 8px" :icon-class="menu.icon"></svg-icon>
      </el-form-item>
      <el-form-item label="是否显示：">
        <el-radio-group v-model="menu.hidden">
          <el-radio :label="0">是</el-radio>
          <el-radio :label="1">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="排序：">
        <el-input v-model="menu.sort"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('menuFrom')">提交</el-button>
        <el-button v-if="!isEdit" @click="resetForm('menuFrom')">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import {
  fetchList,
  createMenu,
  updateMenu,
  getMenu,
  fetchTreeList
} from "@/api/menu";

// import the component
import Treeselect from "@riophae/vue-treeselect";
// import the styles
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

const defaultMenu = {
  title: "",
  parentId: 0,
  name: "",
  icon: "",
  hidden: 0,
  sort: 0
};
export default {
  name: "MenuDetail",
  components: {
    Treeselect
  },
  props: {},
  data() {
    return {
      isEdit: false,
      ismenushow: false,
      selecttreelabel: "",
      defaultProps: {
        children: "children",
        label: "title"
      },
      menu: Object.assign({}, defaultMenu),
      selectMenuList: [],
      rules: {
        title: [
          { required: true, message: "请输入菜单名称", trigger: "blur" },
          {
            min: 2,
            max: 140,
            message: "长度在 2 到 140 个字符",
            trigger: "blur"
          }
        ],
        name: [
          { required: true, message: "请输入前端名称", trigger: "blur" },
          {
            min: 2,
            max: 140,
            message: "长度在 2 到 140 个字符",
            trigger: "blur"
          }
        ],
        icon: [
          { required: true, message: "请输入前端图标", trigger: "blur" },
          {
            min: 2,
            max: 140,
            message: "长度在 2 到 140 个字符",
            trigger: "blur"
          }
        ]
      }
    };
  },
  created() {},
  watch: {
    ismenushow: {
      handler: function(n, o) {
        if (n) {
          this.getSelectMenuList();
        }
      }
    }
  },
  methods: {
    treeselectclick(node) {
      this.menu.parentId = node.id;
    },
    normalizer(node) {
      if (node.children) {
        if (node.children.length == 0) {
          delete node.children;
          return {
            label: node.title
          };
        } else {
          // delete node.children;
          return {
            label: node.title
          };
        }
      } else {
        delete node.children;
        return {
          label: node.title
        };
      }
    },

    handleNodeClick(node) {
      if (node) {
      } else {
        this.welcomeReport.reportId = node.url;
        this.welcomeReport.reportName = node.label;
        this.$refs.selectReport.blur();
        console.log(
          this.welcomeReport.reportName,
          "this.welcomeReport.reportName"
        );
      }
    },

    getSelectMenuList() {
      fetchTreeList().then(response => {
        this.selectMenuList = response.data;
        this.selectMenuList.unshift({ id: 0, title: "无上级菜单" });

        if (this.isEdit) {
          this.menu = this.$root.permission.editmenu;
          this.selecttreelabel = this.$root.permission.editmenu.parentId;
        } else {
          this.menu = Object.assign({}, defaultMenu);
          this.selecttreelabel = 0;
        }
      });
    },
    onSubmit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if (this.isEdit) {
            updateMenu(this.menu.id, this.menu).then(response => {
              this.$message({
                message: "修改成功",
                type: "success",
                duration: 1000
              });
            });
            this.$parent.$parent.isshowdia = false;
            this.$parent.$parent.getList();
          } else {
            createMenu(this.menu).then(response => {
              this.$refs[formName].resetFields();
              this.resetForm(formName);
              this.$message({
                message: "提交成功",
                type: "success",
                duration: 1000
              });
              this.$parent.$parent.isshowdia = false;
              this.$parent.$parent.getList();
            });
          }
        } else {
          this.$message({
            message: "验证失败",
            type: "error",
            duration: 1000
          });
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.menu = Object.assign({}, defaultMenu);
      this.getSelectMenuList();
    }
  }
};
</script>

<style scoped>
</style>
